﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
// std headers

// qt headers
#include <QLabel>
#include <QCheckBox>
#include <QVBoxLayout>

// gcmp headers

// custom headers
#include "SavingEditingProjectComponentDialog.h"
#include "AppService.h"
#include "ProjectLoadedComponentService.h"
#include "GmepLocalDbService.h"
#include "GbmpNew.h"
#include "DebugMessage.h"
#include "GncINetworkCheck.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

namespace gmepgcs
{
    SavingEditingProjectComponentDialog::SavingEditingProjectComponentDialog(QWidget *parent, Qt::WindowFlags flags) :
        MessageDialog(parent, flags),
        m_canUpdateAfterSave(false),
        m_shouldUpdateAfterSave(false)
    {
        Init();
    }

    SavingEditingProjectComponentDialog::~SavingEditingProjectComponentDialog()
    {

    }

    SavingEditingProjectComponentDialog *SavingEditingProjectComponentDialog::s_pInstance = nullptr;
    SavingEditingProjectComponentDialog * SavingEditingProjectComponentDialog::Get(QWidget * parent)
    {
        if (!SavingEditingProjectComponentDialog::s_pInstance)
        {
            SavingEditingProjectComponentDialog::s_pInstance = NEW_AS_QT(SavingEditingProjectComponentDialog,parent);
            SavingEditingProjectComponentDialog::s_pInstance->setObjectName("SaveEdittedDialog");
        }
        else if (SavingEditingProjectComponentDialog::s_pInstance->parentWidget() != parent)
        {
            SavingEditingProjectComponentDialog::s_pInstance->deleteLater();
            SavingEditingProjectComponentDialog::s_pInstance = NEW_AS_QT(SavingEditingProjectComponentDialog,parent);
            SavingEditingProjectComponentDialog::s_pInstance->setObjectName("SaveEdittedDialog");
        }
        return SavingEditingProjectComponentDialog::s_pInstance;
    }

    MessageDialog::MessageDialogResult SavingEditingProjectComponentDialog::Show(const bool &canUpdateAfterSave, bool & shouldUpdateAfterSave,bool isPrivateEnv, QWidget * parent)
    {
        SavingEditingProjectComponentDialog *pDialog = SavingEditingProjectComponentDialog::Get(parent);
        if (!pDialog)
        {
            return SavingEditingProjectComponentDialog::MessageDialogResult::ResultNotSet;
        }

        pDialog->Resume();
        pDialog->m_canUpdateAfterSave = canUpdateAfterSave;
        pDialog->m_isPrivateEnv = isPrivateEnv;
        pDialog->Show();

        pDialog->setModal(true);
        pDialog->exec();

        MessageDialog::MessageDialogResult result = pDialog->GetDialogResult();
        shouldUpdateAfterSave = pDialog->GetShouldUpdateAfterSave();

        return result;
    }

    void SavingEditingProjectComponentDialog::Init()
    {
        // tip label
        QLabel *pLblTip = NEW_AS_QT( QLabel,"<p style='line-height:24px'>" + QString::fromStdWString(GBMP_TR(L"修改内容将更新至工作单元，是否继续保存？")) + "</p>", this);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pLblTip, L"pLblTip为空", L"GMEP", L"2024-01-30");
        pLblTip->setWordWrap(true);
        pLblTip->setAlignment(Qt::AlignLeft);
        pLblTip->setStyleSheet(
            "QLabel{"
            "font-family:\"Microsoft YaHei\";"
            "font-size:12px;"
            "color:rgba(40, 40, 51, 255);"
            "}"
        );

        // will-update checkBox
        m_pCbUpdateAfterSave = NEW_AS_QT( QCheckBox,QString::fromStdWString(GBMP_TR(L"同步到协同项目构件库")), this);
       DBG_WARN_AND_RETURN_VOID_UNLESS(m_pCbUpdateAfterSave, L"m_pCbUpdateAfterSave为空", L"GMEP", L"2024-01-30");
        m_pCbUpdateAfterSave->setObjectName("DoUpdateCheckBox");
        m_pCbUpdateAfterSave->setStyleSheet(
            "QCheckBox{"
            "font-family:\"Microsoft YaHei\";"
            "font-size:12px;"
            "color:rgba(120, 122, 131, 255);"
            "}"
        );
        connect(m_pCbUpdateAfterSave, &QCheckBox::stateChanged, [=](int state) {
            m_shouldUpdateAfterSave = state == Qt::CheckState::Checked;
        });

        // vLayout
        QVBoxLayout *pVLayout = NEW_AS_QT(QVBoxLayout);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pVLayout, L"pVLayout为空", L"GMEP", L"2024-01-30");
        pVLayout->setContentsMargins(16, 16, 16, 16);
        pVLayout->setSpacing(16);
        pVLayout->addWidget(pLblTip);
        pVLayout->addWidget(m_pCbUpdateAfterSave);
        pVLayout->setAlignment(m_pCbUpdateAfterSave, Qt::AlignLeft);

        // central widget
        QWidget *pCentralWidget = NEW_AS_QT(QWidget,this);
        DBG_WARN_AND_RETURN_VOID_UNLESS(pCentralWidget, L"pCentralWidget为空", L"GMEP", L"2024-01-30");
        pCentralWidget->setObjectName("CentralWidget");
        pCentralWidget->setLayout(pVLayout);
        SetCentralWidget(pCentralWidget);

        // dialog self
        setFixedWidth(350);
        setMinimumHeight(100);
        setWindowIcon(QIcon());
        setWindowTitle(QString::fromStdWString(GBMP_TR(L"保存构件")));
        SetButtons(MessageDialogButton::BtnCancel | MessageDialogButton::BtnOk);
    }

    void SavingEditingProjectComponentDialog::Resume()
    {        
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pCbUpdateAfterSave, L"m_pCbUpdateAfterSave为空", L"GMEP", L"2024-01-30");
        m_pCbUpdateAfterSave->setEnabled(false);
        m_pCbUpdateAfterSave->setChecked(false);
    }

    void SavingEditingProjectComponentDialog::Show()
    {
        DBG_WARN_AND_RETURN_VOID_UNLESS(m_pCbUpdateAfterSave, L"m_pCbUpdateAfterSave为空", L"GMEP", L"2024-01-30");
        // 若是公有云需要检查网络，私有化暂不检查
        if (!m_isPrivateEnv && (!gnc::INetworkCheck::Get() || !gnc::INetworkCheck::Get()->CheckNetworkConnection()))
        {
            m_canUpdateAfterSave = false;
            m_shouldUpdateAfterSave = false;
            m_pCbUpdateAfterSave->setText(QString::fromStdWString(GBMP_TR(L"同步到协同项目构件库 -- 没有联网，请检查网络")));
        }
        else
        {
            m_pCbUpdateAfterSave->setText(QString::fromStdWString(GBMP_TR(L"同步到协同项目构件库")));
        }
        m_pCbUpdateAfterSave->setEnabled(m_canUpdateAfterSave);
        m_shouldUpdateAfterSave = m_canUpdateAfterSave;
        m_pCbUpdateAfterSave->setChecked(m_shouldUpdateAfterSave);
    }

    bool SavingEditingProjectComponentDialog::GetShouldUpdateAfterSave()
    {
        return m_shouldUpdateAfterSave;
    }

}